using Godot;
using System;
using System.Collections.Generic;

/// <summary>
/// C#卡牌资源类，用于处理GDScript中定义的Card资源
/// </summary>
public partial class CardResource : Resource
{
    /// <summary>
    /// 卡牌类型枚举
    /// </summary>
    public enum CardType
    {
        Attack,
        Skill,
        Power
    }
    
    /// <summary>
    /// 卡牌稀有度枚举
    /// </summary>
    public enum CardRarity
    {
        Common,
        Rare,
        Legend
    }
    
    /// <summary>
    /// 卡牌目标枚举
    /// </summary>
    public enum CardTarget
    {
        Self,
        SingleEnemy,
        AllEnemies,
        Everyone
    }
    
    // 原始GDScript卡牌资源
    private Resource _gdResource;
    
    // 卡牌属性
    public string Id { get; private set; }
    public CardType Type { get; private set; }
    public CardRarity Rarity { get; private set; }
    public CardTarget Target { get; private set; }
    public int Cost { get; private set; }
    public string TooltipText { get; private set; }
    
    /// <summary>
    /// 从GDScript资源创建C#卡牌资源
    /// </summary>
    /// <param name="gdResource">GDScript卡牌资源</param>
    public CardResource(Resource gdResource)
    {
        _gdResource = gdResource;
        LoadFromGDResource();
    }
    
    /// <summary>
    /// 从GDScript资源加载卡牌数据
    /// </summary>
    private void LoadFromGDResource()
    {
        if (_gdResource == null)
            return;
            
        // 读取基本属性
        Id = _gdResource.Get("id").AsString();
        Cost = _gdResource.Get("cost").AsInt32();
        TooltipText = _gdResource.Get("tooltip_text").AsString();
        
        // 转换枚举类型
        Type = (CardType)_gdResource.Get("type").AsInt32();
        Rarity = (CardRarity)_gdResource.Get("rarity").AsInt32();
        Target = (CardTarget)_gdResource.Get("target").AsInt32();
    }
    
    /// <summary>
    /// 获取原始GDScript资源
    /// </summary>
    /// <returns>GDScript资源</returns>
    public Resource GetGDResource()
    {
        return _gdResource;
    }
    
    /// <summary>
    /// 更新卡牌属性到GDScript资源
    /// </summary>
    public void UpdateGDResource()
    {
        if (_gdResource == null)
            return;
            
        _gdResource.Set("id", Id);
        _gdResource.Set("cost", Cost);
        _gdResource.Set("tooltip_text", TooltipText);
        _gdResource.Set("type", (int)Type);
        _gdResource.Set("rarity", (int)Rarity);
        _gdResource.Set("target", (int)Target);
    }
    
    /// <summary>
    /// 从文件路径加载卡牌资源
    /// </summary>
    /// <param name="path">资源路径</param>
    /// <returns>卡牌资源对象</returns>
    public static CardResource LoadFromPath(string path)
    {
        var resource = ResourceLoader.Load(path);
        if (resource != null)
        {
            return new CardResource(resource);
        }
        return null;
    }
    
    /// <summary>
    /// 保存卡牌资源到文件
    /// </summary>
    /// <param name="path">保存路径</param>
    /// <returns>是否保存成功</returns>
    public bool SaveToFile(string path)
    {
        UpdateGDResource();
        return ResourceSaver.Save(_gdResource, path) == Error.Ok;
    }
} 